package com.ruoyi.system.service.impl;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysAiChat;
import com.ruoyi.system.mapper.SysAiChatMapper;
import com.ruoyi.system.service.ISysAiChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * Description:
 * Author: ZLZ
 * Date: 2025/4/16
 */
@Slf4j
@Service
public class AiChatServiceImpl implements ISysAiChatService {
    private final Generation gen = new Generation();

    @Value("${aliyun.api-key}")
    private String apiKey;

    @Autowired
    private SysAiChatMapper sysAiChatMapper;

    /**
     * 查询AI智能问答记录
     *
     * @param chatId AI智能问答记录主键
     * @return AI智能问答记录
     */
    @Override
    public SysAiChat selectSysAiChatByChatId(Long chatId)
    {
        return sysAiChatMapper.selectSysAiChatByChatId(chatId);
    }

    /**
     * 查询AI智能问答记录列表
     *
     * @param sysAiChat AI智能问答记录
     * @return AI智能问答记录
     */
    @Override
    public List<SysAiChat> selectSysAiChatList(SysAiChat sysAiChat)
    {
        return sysAiChatMapper.selectSysAiChatList(sysAiChat);
    }

    /**
     * 批量删除AI智能问答记录
     *
     * @param chatIds 需要删除的AI智能问答记录主键
     * @return 结果
     */
    @Override
    public int deleteSysAiChatByChatIds(Long[] chatIds)
    {
        return sysAiChatMapper.deleteSysAiChatByChatIds(chatIds);
    }

    /**
     * 删除AI智能问答记录信息
     *
     * @param chatId AI智能问答记录主键
     * @return 结果
     */
    @Override
    public int deleteSysAiChatByChatId(Long chatId)
    {
        return sysAiChatMapper.deleteSysAiChatByChatId(chatId);
    }

    @Override
    public String getAIResponse(String question) throws ApiException, NoApiKeyException, InputRequiredException {
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("You are a helpful assistant.")
                .build();

        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content(question)
                .build();


        GenerationParam param = GenerationParam.builder()
                .apiKey(apiKey)
                .model("qwen-plus")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();

        GenerationResult call = gen.call(param);
        String response = call.getOutput().getChoices().get(0).getMessage().getContent();
        //获取当前登录用户id
        Long userId = SecurityUtils.getUserId();
        //保存对话记录
        SysAiChat sysAiChat = new SysAiChat();
        sysAiChat.setRequest(question);
        sysAiChat.setResponse(response);
        sysAiChat.setUserId(userId);
        sysAiChatMapper.insertSysAiChat(sysAiChat);
        return response;
    }

}